
package Supplier;
import ConnnectData.DataConnect;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
import model.modelTable;
/**
 *
 * @author langtustupid
 */
public class Supplier_Show extends javax.swing.JInternalFrame{

    /** Creates new form Supplier */
    Connection Con;
    Statement Sta;
    ResultSet Res;
    
    public Supplier_Show() {
        initComponents();
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Supplier_Show.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(Supplier_Show.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(Supplier_Show.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(Supplier_Show.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            Con = DataConnect.connectDB();
            Sta  = Con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
   

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField_Search = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbSupplier = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        mnEdit = new javax.swing.JMenu();
        mnAdd = new javax.swing.JMenu();
        mnDelete = new javax.swing.JMenu();
        mnShow = new javax.swing.JMenu();
        mnClose = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Supplier Management");
        setPreferredSize(new java.awt.Dimension(1056, 481));

        jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 24));
        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/supplier_Managent.png"))); // NOI18N

        jLabel2.setText("Supplier Name :");

        jTextField_Search.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextField_SearchKeyReleased(evt);
            }
        });

        tbSupplier.setFont(new java.awt.Font("Tahoma", 0, 12));
        tbSupplier.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbSupplier.setRowHeight(20);
        tbSupplier.setSelectionBackground(new java.awt.Color(255, 153, 255));
        tbSupplier.setUpdateSelectionOnSort(false);
        tbSupplier.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbSupplierMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbSupplier);

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/Search.jpg"))); // NOI18N

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 903, Short.MAX_VALUE)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(299, 299, 299)
                .addComponent(jLabel2)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addGap(18, 18, 18)
                .addComponent(jTextField_Search, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(300, Short.MAX_VALUE))
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(186, 186, 186)
                .addComponent(jLabel1)
                .addContainerGap(150, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(26, 26, 26)
                .addComponent(jLabel1)
                .addGap(27, 27, 27)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField_Search, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 314, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jMenuBar1.setBackground(new java.awt.Color(255, 255, 255));

        mnEdit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/edit.JPG"))); // NOI18N
        mnEdit.setMnemonic('E');
        mnEdit.setText("Edit Supplier ");
        mnEdit.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnEditMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnEdit);

        mnAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/add.gif"))); // NOI18N
        mnAdd.setText(" Add Supplier ");
        mnAdd.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnAddMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnAdd);

        mnDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/delete.png"))); // NOI18N
        mnDelete.setText("Delete Supplier ");
        mnDelete.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnDeleteMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnDelete);

        mnShow.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/star.gif"))); // NOI18N
        mnShow.setText("Show Supplier");
        mnShow.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnShowMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnShow);

        mnClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/exit.png"))); // NOI18N
        mnClose.setText("Close");
        mnClose.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                mnCloseMouseClicked(evt);
            }
        });
        jMenuBar1.add(mnClose);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mnDeleteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnDeleteMouseClicked
        if (tbSupplier.getSelectedRow() != -1) {
            int remove = JOptionPane.showConfirmDialog(this, "Are you sure want delete this Supplier !");
            if (remove == 0) {
                int index = Integer.parseInt(tbSupplier.getValueAt(tbSupplier.getSelectedRow(), 0).toString());
                try {
//                    String sql_delFK = "Delete from Product where Supplier_Id = " + index;
//                    Sta.executeUpdate(sql_delFK);
                    String sql_del = "Update Supplier set Supplier_status = 0 where Supplier_Id=" + index;
                    Sta.executeUpdate(sql_del);
                    Res = Sta.executeQuery("Select * from Supplier where Supplier_status = 1");
                    tbSupplier.setModel(new modelTable(Res));
                } catch (Exception e) {
                    e.printStackTrace();
                }
   
                JOptionPane.showMessageDialog(this, "Success delete ! ");
            }

        } else {
            JOptionPane.showMessageDialog(null, "Ban can chon Supplier muon xoa !");
        }
    }//GEN-LAST:event_mnDeleteMouseClicked

    private void mnAddMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnAddMouseClicked
        new Add_Supplier().setVisible(true);
    }//GEN-LAST:event_mnAddMouseClicked

    private void mnShowMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnShowMouseClicked
       try{
       String sql = "Select Supplier_Id, Supplier_Name,Supplier_Address,Supplier_Phone from Supplier";
       Res = Sta.executeQuery(sql);
       tbSupplier.setModel(new modelTable(Res));
        
    }catch(Exception ex)
    {
        ex.printStackTrace();
    }
    }//GEN-LAST:event_mnShowMouseClicked

    private void tbSupplierMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbSupplierMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_tbSupplierMouseClicked

    private void mnEditMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnEditMouseClicked
        if(tbSupplier.getSelectedRow()!=-1)
        {
        int id = Integer.parseInt(tbSupplier.getValueAt(tbSupplier.getSelectedRow(), 0).toString());
        String Supplier_Name =(tbSupplier.getValueAt(tbSupplier.getSelectedRow(), 1).toString());
        String Supplier_Address =(tbSupplier.getValueAt(tbSupplier.getSelectedRow(), 2).toString());
        String Supplier_Phone =(tbSupplier.getValueAt(tbSupplier.getSelectedRow(), 3).toString());
        Update_Supplier frmUpdateSupplier = new Update_Supplier();
        frmUpdateSupplier.setID(id);
        frmUpdateSupplier.setSupplier_Name(Supplier_Name);
        frmUpdateSupplier.setSupplier_Address(Supplier_Address);
        frmUpdateSupplier.setSupplier_Phone(Supplier_Phone);
        frmUpdateSupplier.setVisible(true);
        }
        else
            JOptionPane.showMessageDialog(null, "You need select muon suppiler Edit");
    }//GEN-LAST:event_mnEditMouseClicked

    private void jTextField_SearchKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextField_SearchKeyReleased
        try {
            Res = Sta.executeQuery("Select *from Supplier where Supplier_Name like '%" + jTextField_Search.getText().trim() + "%'");
            tbSupplier.setModel(new modelTable(Res));
        } catch (Exception e) {
            e.printStackTrace();
        }
             
    }//GEN-LAST:event_jTextField_SearchKeyReleased

private void mnCloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_mnCloseMouseClicked
// TODO add your handling code here:
    int Close=JOptionPane.showConfirmDialog(null, "Are you sure want close  !");
       if(Close==0)
       this.dispose();
}//GEN-LAST:event_mnCloseMouseClicked

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Supplier_Show().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField_Search;
    private javax.swing.JMenu mnAdd;
    private javax.swing.JMenu mnClose;
    private javax.swing.JMenu mnDelete;
    private javax.swing.JMenu mnEdit;
    private javax.swing.JMenu mnShow;
    private javax.swing.JTable tbSupplier;
    // End of variables declaration//GEN-END:variables

}
